﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Entitys;
using Core.Toolkit.DbInitializer;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.SimpleTemp.Entitys.Sys;

namespace Core.SimpleTemp.Repositories.CBS
{
    /// <summary>
    /// 承包商数据初始化
    /// </summary>
    public class CBS_DBinit : DbInitializer
    {
        public CBS_DBinit()
        {
            Sort = 1;
        }
        public override void Initialize(DbContext context, WebAppOptions _webAppOptions)
        {
            var adminUser = context.Set<SysUser>().Where(a => a.LoginName == _webAppOptions.AdminLoginName).FirstOrDefault();
            var sysFactory = context.Set<SysFactory>().ToList();
            var CBS_List = new List<CBS_List>();
            //承包商
            for (int i = 0; i < 20; i++)
            {
                CBS_List.Add(new CBS_List()
                {
                    ContractorCode = i.ToString(),
                    ContractorName = "承包商" + i,
                    CreateFactory = sysFactory[new Random().Next(0, sysFactory.Count())].Id
                });
            }
            context.Set<CBS_List>().AddRange(CBS_List);

            //承包商_人员名录
            var CBS_UserList = new List<CBS_User>();
            for (int i = 0; i < 20; i++)
            {
                CBS_UserList.Add(new CBS_User()
                {
                    Id = Guid.NewGuid(),
                    PersonnelName = "人员名称" + i,
                    CBSListId = CBS_List[new Random().Next(1, 19)].Id,
                    CreateFactory = sysFactory[new Random().Next(0, sysFactory.Count())].Id
                });
            }
            context.Set<CBS_User>().AddRangeAsync(CBS_UserList);

            var cbs_courseEntityList = new List<CBS_Course>();
            //承包商_培训课件
            for (int i = 0; i < 20; i++)
            {
                cbs_courseEntityList.Add(new CBS_Course()
                {
                    TrainingCoursewareName = "培训课件名称" + i,//培训课件名称
                    TrainingCoursewareContent = "培训课件内容" + i,//培训课件内容
                    CreateFactory = sysFactory[new Random().Next(0, sysFactory.Count())].Id
                });

            }
            context.Set<CBS_Course>().AddRange(cbs_courseEntityList);


            //承包商_培训记录
            for (int i = 0; i < 20; i++)
            {
                context.Set<CBS_TrainRecord>().Add(new CBS_TrainRecord()
                {
                    CBSCourseId = (Guid?)cbs_courseEntityList[new Random().Next(0, 19)].Id,//培训课件ID
                    Trainer = adminUser.Id,//培训人
                    TrainingDate = DbInitializerHelp.GetRandomDate(),//培训日期
                    CBS_TrainRecordUser = new List<CBS_TrainRecordUser>() {
                        new CBS_TrainRecordUser() { UserId = CBS_UserList[new Random().Next(0, 19)].Id },
                        new CBS_TrainRecordUser() { UserId = CBS_UserList[new Random().Next(0, 19)].Id },
                        new CBS_TrainRecordUser() { UserId = CBS_UserList[new Random().Next(0, 19)].Id },
                        new CBS_TrainRecordUser() { UserId = CBS_UserList[new Random().Next(0, 19)].Id },
                        new CBS_TrainRecordUser() { UserId = CBS_UserList[new Random().Next(0, 19)].Id },
                    },
                    Remarks = "备注" + i,//备注
                    AttachmentId = null,//附件Annex
                    CreateFactory = sysFactory[new Random().Next(0, sysFactory.Count())].Id
                });
            }


            //承包商_培训记录
            for (int i = 0; i < 10; i++)
            {
                var Fraction = new Random().Next(50, 99);
                context.Set<CBS_ExamRecord>().Add(new CBS_ExamRecord()
                {
                    TrainRecordId = (Guid?)cbs_courseEntityList[new Random().Next(0, 19)].Id,//培训课件ID
                    Examiner = CBS_UserList[new Random().Next(0, 19)].Id,//考核人
                    ExaminationDate = DbInitializerHelp.GetRandomDate(),//考试日期
                    DegreeOfDifficulty = new Random().Next(1, 2).ToString(),//难易程度
                    Fraction = Fraction,//分数
                    CBSListId = CBS_List[new Random().Next(0, 19)].Id,//承包商
                    IsQualified = (Fraction > 60).ToString(),//是否合格IsPass
                    Remarks = "备注" + i,//备注
                    AttachmentId = null,//附件Annex
                    CreateFactory = sysFactory[new Random().Next(0, sysFactory.Count())].Id,
                    UpdateTime = DateTime.Now,
                    UpdateUser = adminUser.Id
                }); ;
            }


            //违章记录
            for (int i = 0; i < 10; i++)
            {
                context.Set<CBS_BreakRules>().Add(new CBS_BreakRules()
                {
                    CBSListId = CBS_List[new Random().Next(0, 19)].Id,//承包商
                    ViolatorsID = CBS_UserList[new Random().Next(0, 19)].Id,//违规人ID
                    ViolationDate = DbInitializerHelp.GetRandomDate(),//违规日期
                    DiscoveringPeopleID = adminUser.Id,//发现人ID
                    ViolationClause = "违规条款" + i,//违规条款
                    ViolationInstructions = "违规情况说明" + i,//违规情况说明
                    Remarks = "备注" + i,//备注
                    ProcessingOpinion = "处理意见" + 1,//处理意见
                    ContractorQZ = null,//承包商违章签字
                    ProcessingResult = new Random().Next(1, 3).ToString(),
                    CreateFactory = sysFactory[new Random().Next(0, sysFactory.Count())].Id,
                    UpdateTime = DateTime.Now,
                    UpdateUser = adminUser.Id
                });
            }
            context.SaveChanges();

        }
    }
}
